package com.yixun.qfbao.hander;

import com.yixun.qfbao.apidto.GetIndexCommissionStatisticsResDto;
import com.yixun.qfbao.apidto.GetPremisesCommissionReqDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.dto.IndexCommissionStatisticsDto;
import com.yixun.qfbao.service.ReportCommissionService;
import com.yixun.qfbao.vo.GetPremisesCommissionReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description
 * @Date 2017/11/28
 * @Author sangtao
 * @Computer Administrator
 */
@Service("getPremisesCommissionHander")
public class GetPremisesCommissionHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(GetPremisesCommissionHander.class);

    @Autowired
    private ReportCommissionService reportCommissionService;

    @SuppressWarnings("rawtypes")
    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        GetPremisesCommissionReqVo req = (GetPremisesCommissionReqVo)reqDto;
        GetPremisesCommissionReqDto reqDto1 = req.getParams();
        BaseResponseDTO<GetIndexCommissionStatisticsResDto> res = new BaseResponseDTO<GetIndexCommissionStatisticsResDto>();
        GetIndexCommissionStatisticsResDto resDto = new GetIndexCommissionStatisticsResDto();
        try {
            List<Long> premisesIds = new ArrayList<Long>();
            if(StringUtils.isNotBlank(reqDto1.getPremisesId())){
                premisesIds.add(Long.parseLong(reqDto1.getPremisesId()));
            }else{
                String pid =reqDto1.getPremisesIds();
                if(StringUtils.isBlank(pid)){
                    resDto.setResult(ResultEnum.SUCCESS.getNo());
                    resDto.setData(new IndexCommissionStatisticsDto());
                    res.setResData(resDto);
                    res.setResult(resDto.getResult());
                    return res;
                }
                String[] pids = pid.split(",");
                for(int i = 0; i < pids.length; i++){
                    premisesIds.add(Long.parseLong(pids[i]));
                }
            }
            IndexCommissionStatisticsDto dto = new IndexCommissionStatisticsDto();
            dto.setPremisesIds(premisesIds);
            IndexCommissionStatisticsDto a = reportCommissionService.getIndexCommissionStatistics(dto);
            resDto.setData(a);
            resDto.setResult(ResultEnum.SUCCESS.getNo());
        } catch (Exception e) {
            logger.error("GetPremisesCommissionHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("获取楼盘佣金失败!");
        }
        res.setResData(resDto);
        res.setResult(resDto.getResult());
        return res;
    }
}
